//
//  QuestionViewController.swift
//  RabbleWabble
//
//  Created by TRS-Mobile on 2021/9/7.
//

import UIKit

class QuestionViewController: UIViewController {

    var questionGroup: QuestionGroup! {
        didSet {
            navigationItem.title = questionGroup.title
        }
    }
    var questionIndex = 0
    var correctCount = 0
    var incorrectCount = 0
    var questionView: QuestionView! {
        guard isViewLoaded else {
            return .none
        }
        return (view as! QuestionView)
    }
    
    weak var delegate: QuestionViewControllerDelegate?
    
    var questionStrategy: QuestionStrategy! {
        didSet {
            navigationItem.title = questionStrategy.title
        }
    }
    
    private lazy var questionIndexItem: UIBarButtonItem = {
        let item = UIBarButtonItem(title: "", style: .plain, target: .none, action: .none)
        item.tintColor = .black
        navigationItem.rightBarButtonItem = item
        return item
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupCancelButton()
        showQuestion()
    }
    
    private func setupCancelButton() {
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "ic_menu"), landscapeImagePhone: .none, style: .plain, target: self, action: #selector(handleCancelPressed(_:)))
    }
    
    @objc
    private func handleCancelPressed(_ sender: UIButton) {
//        delegate?.questionViewController(self, didCancel: questionGroup, at: questionIndex)
        delegate?.questionViewController(self, didCancel: questionStrategy)
    }
    
    private func showQuestion() {
//        let question = questionGroup.questions[questionIndex]
//        questionView.answerLabel.text = question.answer
//        questionView.hintLabel.text = question.hint
//        questionView.promptLable.text = question.prompt
//        questionView.answerLabel.isHidden = true
//        questionView.hintLabel.isHidden = true
//        questionIndexItem.title = "\(questionIndex + 1)/" + "\(questionGroup.questions.count)"
        
        let question = questionStrategy.currentQuestion()
        questionView.answerLabel.text = question.answer
        questionView.hintLabel.text = question.hint
        questionView.promptLable.text = question.prompt
        questionView.answerLabel.isHidden = true
        questionView.hintLabel.isHidden = true
        questionIndexItem.title = questionStrategy.questionIndexTitle()
    }
    
    @IBAction func toggleAnswerLabels(_ sneder: Any) {
        questionView.answerLabel.isHidden = !questionView.answerLabel.isHidden
        questionView.hintLabel.isHidden = !questionView.hintLabel.isHidden
    }

    @IBAction func handleCorrect(_ sender: Any) {
//        correctCount += 1
//        questionView.correctCountLabel.text = "\(correctCount)"
        let question = questionStrategy.currentQuestion()
        questionStrategy.makeQuestionCorrect(question)
        questionView.correctCountLabel.text = "\(questionStrategy.correctCount)"
        showNextQuestion()
    }
    
    @IBAction func handleIncorrect(_ sender: Any) {
//        incorrectCount += 1
//        questionView.incorrentCountLabel.text = "\(incorrectCount)"
        let question = questionStrategy.currentQuestion()
        questionStrategy.makeQuestionIncorrect(question)
        questionView.incorrentCountLabel.text = "\(questionStrategy.incorrectCount)"
        showNextQuestion()
    }

    func showNextQuestion() {
//        questionIndex += 1
//        guard questionIndex < questionGroup.questions.count else {
//            delegate?.questionViewController(self, didComplete: questionGroup)
//            return
//        }
        guard questionStrategy.advanceToNextQuestion() else {
            delegate?.questionViewController(self, didComplete: questionStrategy)
            return
        }
        showQuestion()
    }
}

protocol QuestionViewControllerDelegate: AnyObject {
    
    func questionViewController(_ vc: QuestionViewController,
                                didCancel questionGroup: QuestionGroup,
                                at questionIndex: Int) -> Void
    
    func questionViewController(_ vc: QuestionViewController,
                                didCancel questionStrategy: QuestionStrategy) -> Void
    func questionViewController(_ vc: QuestionViewController,
                                didComplete questionGroup: QuestionGroup) -> Void
    func questionViewController(_ vc: QuestionViewController,
                                didComplete questionStrategy: QuestionStrategy) -> Void
}

extension QuestionViewControllerDelegate {
    
    func questionViewController(_ vc: QuestionViewController,
                                didCancel questionGroup: QuestionGroup,
                                at questionIndex: Int) -> Void {}
    
    func questionViewController(_ vc: QuestionViewController,
                                didCancel questionStrategy: QuestionStrategy) -> Void {}
    func questionViewController(_ vc: QuestionViewController,
                                didComplete questionGroup: QuestionGroup) -> Void {}
    func questionViewController(_ vc: QuestionViewController,
                                didComplete questionStrategy: QuestionStrategy) -> Void {}
}
